- Consider the following C++ program.
#include <iostream>
using namespace std;
int fun(int &x, int y) {
if (y <= 0) return x;
x = x + 1;
y = y + 1;
cout << x << y << endl;
return x * y;
}
int main() {
int x = 5, y = -1;
cout << fun(x, y) << endl; // line a
fun(x, 1); // line b
fun(y, 1); // line c
fun(y, x); // line d
cout << fun(x, 2) << endl; // line e
return 0;
}
What is the output from the program at each of the following lines:
- line a:
- line b:
- line c:
- line d:
- line e:
- Consider the following C++ program.
#include <iostream>
using namespace std;
int fun(int &x, int &y) {
if (y <= 0) return x;
x = x + 2;
cout << x << y << endl;
return x * y;
}
int main() {
int x = 2, y = 0;
cout << fun(x, y) << endl; // line a
fun(y, x); // line b
fun(x, y); // line c
fun(y, x); // line d
cout << fun(x, y) << endl; // line e
return 0;
}
What is the output from the program at each of the following lines:
- line a:
- line b:
- line c:
- line d:
- line e:
- Write a complete C++ program using the main function given below. Write a function called sort that sorts three integer parameters into decreasing order. For example, a program that uses the function sort follows.
int main() {
int a = 2, b = 7, c = 1;
sort(a, b, c);
cout << a << b << c << endl; // prints 721
return 0;
}
- Write a complete C++ program that converts Eastern time to a different time zone.
- The inputs are (time in Eastern time):
- the current hour (in 24 hour time, so 1:00 pm is 13:00)
- the current minutes
- timezone (Pacific, Mountain, Central)
- Pacific time is -3 from Eastern, Mountain is -2, and Central is -1.
- Your function should convert the values in the hour variable based on the time zone entered.
- Your program should use the main function given below, though you can add onto it if you would like to expand your code.
int main() {
int hour, minutes;
string zone;
cout << "What is the current hour? ";
cin >> hour;
cout << "What is the current minutes? ";
cin >> minutes;
cout << "Enter time zone (Pacific, Mountain, Central): ";
cin >> zone;
timeZoneConvert(hour, zone);
cout << "The current time in " << zone << " is " << hour << ":" << minutes << endl;
return 0;
}
Sample run of program:
What is the current hour? 1
What is the current minutes? 56
Enter time zone (Pacific, Mountain, Central): Pacific
The current time in Pacific is 22:56
Another sample run of program:
What is the current hour? 11
What is the current minutes? 37
Enter time zone (Pacific, Mountain, Central): Mountain
The current time in Mountain is 9:37
Another sample run of program:
What is the current hour? 14
What is the current minutes? 25
Enter time zone (Pacific, Mountain, Central): Central
The current time in Central is 13:25
- Write a complete C++ program using the main function given below. In this program, write a function called digitMatch. The function has three integer parameters: first, second, and third that are positive and have the same number of digits. It returns the number of positions where their digits match. For example, if teh function was applied to 17345, 97813 and 17313 it would return one because only the second digits match (the 7 that is second from the left).
You can assume that the user gives you inputs matching the input requirements.
int main() {
cout << digitMatch(168, 567, 767) << endl; // prints 1
cout << digitMatch(143, 243, 343) << endl; // prints 2
return 0;
}